(function(exports) {
	var gc = gameConfig,
		w = gc.width,
		h = gc.height,
		g = gc.g,
		sp = gc.speed,
		gapX = gc.pipeGapX,
		pipeWidth = gc.pipeWidth;


	exports.Game = function(container, viewport) {
		window.requestAnimationFrame = window.requestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.msRequestAnimationFrame ||
			window.mozRequestAnimationFrame;

		//画布
		var container = document.querySelector(container);
		var canvas = document.querySelector(viewport);
		var ctx = canvas.getContext('2d');
		container.style.position = "relative";
		canvas.style.position = "absolute";
		canvas.style.left = 0;
		canvas.style.top = 0;
		canvas.width = w;
		canvas.height = h;

		//数据
		var timer;
		var status = -1;
		var birds = [];
		var pipes = [];
		var keyPress = [];
		var totalDist = 0;
		var lastPipeDist = gc.pipeGapX;

		//键盘事件
		document.onkeydown = function(e) {
			console.log(e.keyCode);
			keyPress[e.keyCode] = true;
			if (status == 0) {
				status = 1;
				var old = gc.now;
				gc.now = +new Date();
				gc.secSpan = (gc.now - old) / 100;
				run();
			}
		};

		//运行
		function run() {
			//console.log(status);
			if (status == 1)
				timer = requestAnimationFrame(run);
			else
				window.cancelAnimationFrame(timer);
			update();
			collide();
			draw();
		}

		//更新
		function update() {
			//时间间隔
			var old = gc.now;
			gc.now = +new Date();
			gc.secSpan = (gc.now - old) / 100;
			//距离
			var dist = sp * gc.secSpan;
			totalDist += dist;
			lastPipeDist += dist;
			if (lastPipeDist >= gc.pipeGapX) {
				var obj = Pipe.createPair();
				pipes.push(obj.top);
				pipes.push(obj.bottom);
				lastPipeDist = 0;
			}

			for (var i = 0; i < birds.length; i++) {
				birds[i].update(keyPress);
				if (birds[i].pos.x + birds[i].size < 0) {
					birds.splice(i, 1);
				}
			}

			for (var i = 0; i < pipes.length; i++) {
				pipes[i].update();
				if (pipes[i].pos.x + pipeWidth < 0) {
					pipes.splice(i, 1);
				}
			}
		}

		//碰撞
		function collide() {
			var len = birds.length;
			var pipeLen = pipes.length;
			for (var i = 0; i < len; i++) {
				for (var j = 0; j < pipeLen; j++) {
					if (birds[i].alive && birds[i].hitTest(pipes[j])) {
						birds[i].keyMap = {};
						birds[i].alive = false;
						vibrate();
						//status = 2;
					}
				}
			}
		}

		//绘制
		function draw() {
			ctx.clearRect(0, 0, w, h);
			//console.log(totalDist);
			ctx.fillText("距离：" + totalDist.toFixed(), 10, h - 10);
			var len = birds.length;
			for (var i = 0; i < len; i++) {
				birds[i].draw(ctx);
			}

			len = pipes.length;
			for (var i = 0; i < len; i++) {
				pipes[i].draw(ctx);
			}
		}

		//震动
		function vibrate() {
			if ($) {
				$(canvas).animate({
					left: -2
				}, 50).animate({
					left: 2
				}, 50).animate({
					left: -2
				}, 50).animate({
					left: 2
				}, 50).animate({
					left: -2
				}, 50).animate({
					left: 2
				}, 50).animate({
					left: 0
				}, 30);
			}
		}

		return {
			init: function() {
				/*birds.push(new Bird(new Vec2(gc.birdPosX, h / 2), {
					'flap': 32
				}));*/
				birds.push(new Bird(new Vec2(gc.birdPosX + 20, h / 2 - 50), {
					'flap': 70
				}));
				birds.push(new Bird(new Vec2(gc.birdPosX, h / 2 - 50), {
					'flap': 74
				}));
				draw();
				ctx.fillText("空格键开始", 10, 20);
				status = 0;
			},
			stop: function() {
				status = 2;
			}
		}
	}
})(window);